package stringdemo;

import java.util.Random;
import java.util.Scanner;

public class StringDemo04 {
    public static void main(String[] args) {
        //读题拆解法

        //1.定义两个变量记录正确的用户名和密码

        String rightUsername = "用户名";
        String rightPassword ="123456";
        System.out.println(getSecurityCode());




        //2.键盘录入用户名和密码
        //2种for循环书写方式：
        //1.快捷键ctrl+alt+t
        //2.循环条件.fori

        Scanner sc = new Scanner(System.in);
        for (int i=0; i < 5; i++) {
            System.out.println("请输入用户名");
            String username = sc.next();
            System.out.println("请输入密码");
            String password = sc.next();
            System.out.println("请输入验证码");
            final String getSecurityCode = sc.next();
            if (username.equals(rightUsername) && password.equals(rightPassword)){
                
                System.out.println("用户登录成功");
                break;
            }else{
                if (i==4){
                    //最后一次机会也输入错误，提示账号被锁定
                    System.out.println("账户"+rightUsername+"被锁定，请联系刘顺国解决。电话:15315572328");
                }else{
                    System.out.println("用户第"+(i+1)+"次登录失败，用户名或密码有误，还剩"+(4-i)+"次机会!");
                }

            }
        }

    }
    public static String getSecurityCode(){
        char[]chs=new char[52];
        for(int j=0;j<chs.length;j++){
            //ASCII码表
            if(j<=25){
                //添加小写字母
                chs[j]=(char)(97+j);
            }else{
                //添加大写字母
                chs[j]=(char)(65+j-26);
            }
        }
        String result = "";
        Random r = new Random();
        for(int j=0;j<3;j++) {
            int randomIndex = r.nextInt(chs.length);
            result = result + chs[randomIndex];
        }
        int number = r.nextInt(10);
        result = result+number;
        return result;
    }
}
